//
//  AppUpdateManager.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/5/9.
//

import Foundation
import YPBedrock

class WmcAppUpdateManager: NSObject {
    /// 单例实例化
    static let shared: WmcAppUpdateManager = {
        let manager = WmcAppUpdateManager()
        return manager
    }()

    /// 检查更新
    static func CheckUpdate(showToast: Bool = false) {
        IsNewVersion() { isNew, model in
            if isNew {
                if showToast {
                    WmcHudManager.toast("当前已经是最新版本")
                }
            } else {
                let alertView = WmcAppUpdateAlert()
                alertView.configModel(model)
                alertView.show()
            }
        }
    }

    /// 校验是否为新版本
    static func IsNewVersion(block: @escaping YPBlock.TwoParam<Bool, WmcVersionInfoModel>) {
        WmcOtherAPI.fetchVersionInfo().subscribe(onNext: { model in
            let currentVersion = YPWmcManager.shared.version
            let currentNum = currentVersion.yp.replacing(".", with: "").yp.int
            let networkNum = model.versionCode
            guard currentNum < networkNum else {
                block(true, model)
                return
            }
            block(false, model)

        }).disposed(by: shared.rx.disposeBag)
    }
}
